package cn.agiledata.bank.account.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.dao.ActionTypeTemplate;
import cn.agiledata.bank.common.dao.HibernateDaoUtil;
import cn.agiledata.bank.common.service.AccountService;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AccountSetup;
import cn.agiledata.bank.hibernateBean.AccountSetupD;
import cn.agiledata.bank.hibernateBean.AccountSetupInterf;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.myspace.form.UserSetupActionForm;

public class AccountServiceImpl
	implements AccountService {

	private String[] getValidIdsFromObjects(Object[] objs){
		String[] returnStr = new  String[objs.length];
		for (int i=0;i<objs.length;i++){
			AccountSetupD asd = (AccountSetupD)objs[i];
			if (asd.getValidFlag().equals(Constant.VALID))
				returnStr[i] = asd.getActionId();
		}
		return returnStr;
	}
	private Long[] permitActionType = new Long[]{
			new Long(Constant.ACTION_TYPE_A),//充值
			new Long(Constant.ACTION_TYPE_C),//银商信间交易
			new Long(Constant.ACTION_TYPE_D),//提现
			new Long(Constant.ACTION_TYPE_F),//银行付款给对方银商信			
	};
	
	private ActionTypeTemplate att;
	private AccountTemplateInter ati ;
	private HibernateDaoUtil hdu;
	
	public void setAtt(ActionTypeTemplate att) {
		this.att = att;
	}

	public void setHdu(HibernateDaoUtil hdu) {
		this.hdu = hdu;
	}

	public void setAti(AccountTemplateInter ati) {
		this.ati = ati;
	}

	public void initialize(String subAccountId) {
		// TODO Auto-generated method stub
		SubAccount sub = ati.getSubAccount(subAccountId);
		saveSubAccountSetup(sub);
	}

	public boolean ifExistSetup(Account acco){
		SubAccount sub = (SubAccount)acco.getSubAccounts().iterator().next();
		return ifExistSetup(sub);		
	}
	
	public boolean ifCanTelephone(SubAccount sub,String moduleType){
		return CanTeleOrMessage(sub,moduleType,Constant.ACCOUNT_SETUP_TYPE_A);
	}
	
	public boolean ifCanMessage(SubAccount sub,String moduleType){
		return CanTeleOrMessage(sub,moduleType,Constant.ACCOUNT_SETUP_TYPE_B);
	}
	
	private boolean CanTeleOrMessage(SubAccount sub,
			String moduleType,String teleOrMessage){
		return hdu.ifExistObj("from AccountSetupD s where " +
				"s.setSeq.account.id="+sub.getId()+
				" and s.setSeq.setId = '"+teleOrMessage+"'"+
				" and s.actionId= '"+moduleType+"'"+
				" and s.validFlag = 'Y'");
	}
	public boolean ifExistSetup(SubAccount sub){
		return hdu.ifExistObj("from AccountSetup s where s.account.id = " +
				sub.getId());
	}
	private void saveSubAccountSetup(SubAccount sub) {
		if (!this.ifExistSetup(sub)) {
			List setUpList = pickUpSetUp(sub, new String[] {
					Constant.ACCOUNT_SETUP_TYPE_A,
					Constant.ACCOUNT_SETUP_TYPE_B });
			hdu.saveOrUpdateList(setUpList);
			hdu.getHibernateTemplate().flush();
		}
	}

	public void initialize(Account acco){
		Iterator iter = acco.getSubAccounts().iterator();
		while (iter.hasNext()) {
			SubAccount sub = (SubAccount) iter.next();
			saveSubAccountSetup(sub);
		}
	}
	public AccountSetupInterf getAccountSetup(String setupId) {
		// TODO Auto-generated method stub
		return 
		(AccountSetup)hdu.getObjectById(new AccountSetup(),new Long(setupId));
	}

	public void initialize(AccountSetupInterf account) {
		// TODO Auto-generated method stub
		
	}

	public void saveOrUpdateAccountSetup(AccountSetupInterf SaveObject) {
		// TODO Auto-generated method stub

	}
	public List getAccountModelTypeList() {
		// TODO Auto-generated method stub
		return att.getModelTypeByIds(permitActionType);
	}
	private List pickUpSetUp(SubAccount sub,String[] setIds){
		List result = new ArrayList();
		for (int i=0;i<setIds.length;i++){
			AccountSetup as = new AccountSetup();
			as.setAccount(sub);
			as.setSetId(setIds[i]);
			as.setValidFlag(Constant.VALID);
			as.setSetupds(pickUpSetUpD(as,permitActionType));
			WeatherUtil.fillCreateAndUpdateInfo(as, sub.getId().toString());
			result.add(as);
		}
		return result;
	}
	private Set pickUpSetUpD(AccountSetup as,Long[] actionIds){
		Set setupd = new HashSet();
		for (int i=0;i<actionIds.length;i++){
			AccountSetupD asd = new AccountSetupD();
			asd.setActionId(actionIds[i].toString());
			asd.setSetSeq(as);
			asd.setValidFlag(Constant.NOT_VALID);
			WeatherUtil.fillCreateAndUpdateInfo(asd, as.getAccount().getId().toString());
			setupd.add(asd);
		}
		return setupd;
	}
	public void saveSubAccountSetup(UserSetupActionForm form, 
			String accountId) {
		// TODO Auto-generated method stub
//		  SubAccount acc = 
//			  (SubAccount)hdu.loadObjectById(new SubAccount(), new Long(accountId));
//		  Object[] setups = acc.getSetups().toArray();
//		  AccountSetup telephoneSetup = (AccountSetup)setups[0];
//		  String[] tele = form.getTele();
//		  pickUpSaveInfo(telephoneSetup, tele);
//		  
//		  AccountSetup messageSetup = (AccountSetup)setups[1];
//		  String[] message = form.getMessage();
//		  pickUpSaveInfo(messageSetup, message);
//		  
//		  hdu.saveOrUpdateObject(acc);
	}

	private void pickUpSaveInfo(AccountSetup telephoneSetup, String[] tele) {
		Iterator iter = telephoneSetup.getSetupds().iterator();
		  while(iter.hasNext()){
			  AccountSetupD asd = (AccountSetupD)iter.next();
			  if (StringUtils.indexOfAny(asd.getActionId(), tele) ==0){
				  asd.setValidFlag(Constant.VALID);
				  
			  }else{
				  asd.setValidFlag(Constant.NOT_VALID);
			  }
		  }
	}
	public void pickUpSetupForm(UserSetupActionForm form, String subId) {
		// TODO Auto-generated method stub
//	  SubAccount acc = (SubAccount)hdu.getObjectById(new SubAccount(), new Long(subId));
//	  Object[] setups = acc.getSetups().toArray();
//	  AccountSetup telephoneSetup = (AccountSetup)setups[0];
//	  form.setTele(getValidIdsFromObjects(
//			  telephoneSetup.getSetupds().toArray()));
//	  
//	  AccountSetup messageSetup = (AccountSetup)setups[1];
//	  form.setMessage(getValidIdsFromObjects(
//			  messageSetup.getSetupds().toArray()));
	}

	public void saveAccountMessage(Account account) {
		hdu.saveOrUpdateObject(account);		
	}
}
